package com.greentreecs.fms.service.impl;

import com.greentreecs.fms.exception.BadParamException;
import com.greentreecs.fms.mapper.QueryIncDetailRecMapper;
import com.greentreecs.fms.pojo.dto.IncDetailRecDTO;
import com.greentreecs.fms.pojo.param.CreateIncDetailParam;
import com.greentreecs.fms.pojo.param.QueryIncDetailParam;
import com.greentreecs.fms.pojo.param.UpdateIncDetailParam;
import com.greentreecs.fms.service.IncomeAnalyseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 刀氏春秋
 * @date 2025/2/18
 */
@Service
public class IncomeAnalyseServiceImpl implements IncomeAnalyseService {
    @Autowired
    private QueryIncDetailRecMapper queryIncDetMapper;

    @Override
    public void createNewIncomeDetail(CreateIncDetailParam param) throws SQLException, BadParamException {
        // 首先进行参数校验，房间号必须为4位数字
        if (Integer.parseInt(param.getRoomNo()) < 1e3 || Integer.parseInt(param.getRoomNo()) >= 1e4) {
            throw new BadParamException("房间号必须为4位数字");
        } else {
            // 若无异常则表示数据插入成功，否则失败
            queryIncDetMapper.insertIncDetailRec(param);
        }
    }

    @Override
    public List<IncDetailRecDTO> queryIncomeDetail(QueryIncDetailParam param) {
        // 房间号与入住人姓名遵循模糊匹配原则，需要在前后添加通配符
        // 若内容为空，则跳过这一步骤，因为匹配条件未设置
        if (param.getRoomNo() != null && !param.getRoomNo().equals(""))
            param.setRoomNo("%" + param.getRoomNo() + "%");
        if (param.getCustomer() != null && !param.getCustomer().equals(""))
            param.setCustomer("%" + param.getCustomer() + "%");
        // 执行查询
        return queryIncDetMapper.selectIncDetailRec(param);
    }

    @Override
    public Integer deleteIncomeDetail(List<Integer> params) {
        return queryIncDetMapper.deleteIncDetailRec(params);
    }

    @Override
    public Integer updateIncomeDetail(UpdateIncDetailParam param) {
        return queryIncDetMapper.updateIncDetailRec(param);
    }
}
